home *** CD-ROM | disk | FTP | other *** search
/ PCGUIA 114 / PC Guia 114.iso / Software / Utils / The Gimp 2.2.1 / gimp-help-2-0.6-setup.exe / {app} / share / gimp / 2.0 / help / en / ch02s10s06.html < prev    next >
Encoding:
Extensible Markup Language  |  2004-12-19  |  10.6 KB  |  271 lines

  1. <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4.   <head>
  5.     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  6.     <title>10.6.┬áExtending The Text Box Script</title>
  7.     <link rel="stylesheet" href="gimp-help-plain.css" type="text/css" />
  8.     <link rel="stylesheet" href="gimp-help-screen.css" type="text/css" />
  9.     <meta name="generator" content="DocBook XSL Stylesheets V1.66.1" />
  10.     <link rel="start" href="index.html" title="GIMP User Manual" />
  11.     <link rel="up" href="ch02s10.html" title="10.┬áA Script-Fu Tutorial" />
  12.     <link rel="prev" href="ch02s10s05.html" title="10.5.┬áGiving Our Script Some Guts" />
  13.     <link rel="next" href="ch02s11.html" title="11.┬áGetting Unstuck" />
  14.   </head>
  15.   <body>
  16.     <div xmlns="" class="navheader">
  17.       <table width="100%" summary="Navigation header">
  18.         <tr>
  19.           <th colspan="3" align="center" id="chaptername">10.┬áA Script-Fu Tutorial</th>
  20.         </tr>
  21.         <tr>
  22.           <td width="20%" align="left"><a accesskey="p" href="ch02s10s05.html">Prev</a>┬á</td>
  23.           <th width="60%" align="center" id="sectionname">10.6.┬áExtending The Text Box Script</th>
  24.           <td width="20%" align="right">┬á<a accesskey="n" href="ch02s11.html">Next</a></td>
  25.         </tr>
  26.       </table>
  27.       <hr />
  28.     </div>
  29.     <div class="sect2" lang="en" xml:lang="en">
  30.       <div class="titlepage">
  31.         <div>
  32.           <div>
  33.             <h3 class="title"><a id="id3431098"></a>10.6.┬áExtending The Text Box Script</h3>
  34.           </div>
  35.         </div>
  36.       </div>
  37.       <div class="simplesect" lang="en" xml:lang="en">
  38.         <div class="titlepage">
  39.           <div>
  40.             <div>
  41.               <h4 class="title"><a id="id3431105"></a>Handling Undo Correctly</h4>
  42.             </div>
  43.           </div>
  44.         </div>
  45.         <p>
  46.         When creating a script, you want to give your users the
  47.         ability to undo their actions, should they make a
  48.         mistake. This is easily accomplished by calling the functions
  49.         <tt class="code">gimp-undo-push-group-start</tt> and 
  50.         <tt class="code">gimp-undo-push-group-end</tt> around
  51.         the code that manipulates the image. You can think of them as
  52.         matched statements that let Gimp know when to start and stop
  53.         recording manipulations on the image, so that those
  54.         manipulations can later be undone. 
  55.       </p>
  56.         <p>
  57.         If you are creating a new image entirely, it doesn't make
  58.         sense to use these functions because you're not changing an
  59.         existing image. However, when you are changing an existing
  60.         image, you most surely want to use these functions. 
  61.       </p>
  62.         <p>
  63.         Undoing a script works nearly flawlessly when using these
  64.         functions. 
  65.       </p>
  66.       </div>
  67.       <div class="simplesect" lang="en" xml:lang="en">
  68.         <div class="titlepage">
  69.           <div>
  70.             <div>
  71.               <h4 class="title"><a id="id3431142"></a>Extending The Script A Little More</h4>
  72.             </div>
  73.           </div>
  74.         </div>
  75.         <p>
  76.         Now that we have a very handy-dandy script to create text
  77.         boxes, let's add two features to it: 
  78.       </p>
  79.         <div class="itemizedlist">
  80.           <ul type="disc">
  81.             <li>
  82.               <p>
  83.           Currently, the image is resized to fit exactly around the
  84.           text -- there's no room for anything, like drop shadows or
  85.           special effects (even though many scripts will automatically
  86.           resize the image as necessary). Let's add a buffer around
  87.           the text, and even let the user specify how much buffer to
  88.           add as a percentage of the size of the resultant text. 
  89.         </p>
  90.             </li>
  91.             <li>
  92.               <p>
  93.           This script could easily be used in other scripts that work
  94.           with text. Let's extend it so that it returns the image and
  95.           the layers, so other scripts can call this script and use
  96.           the image and layers we create. 
  97.         </p>
  98.             </li>
  99.           </ul>
  100.         </div>
  101.       </div>
  102.       <div class="simplesect" lang="en" xml:lang="en">
  103.         <div class="titlepage">
  104.           <div>
  105.             <div>
  106.               <h4 class="title"><a id="id3431179"></a>Modifying The Parameters And The Registration Function</h4>
  107.             </div>
  108.           </div>
  109.         </div>
  110.         <p>
  111.         To let the user specify the amount of buffer, we'll add a
  112.         parameter to our function and the registration function: 
  113.       </p>
  114.         <pre class="programlisting">
  115.         (define (script-fu-text-box inTest inFont inFontSize inTextColor)
  116.         (let*
  117.               (
  118.                  ; define our local variables
  119.                  ; create a new image:
  120.                  (theImageWidth  10)
  121.                  (theImageHeight 10)
  122.                  (theImage (car 
  123.                                 (gimp-image-new
  124.                                  theImageWidth
  125.                                  theImageHeight
  126.                                  RGB
  127.                                 )
  128.                            )
  129.                  )
  130.                  (theText)          ;a declaration for the text
  131.                                     ;we create later
  132.  
  133.                  (theBuffer)        ;<span class="bold"><b>added</b></span>
  134.                  (theLayer 
  135.                            (car
  136.                                (gimp-layer-new
  137.                                 theImage
  138.                                 theImageWidth
  139.                                 theImageHeight
  140.                                 RGB_IMAGE
  141.                                 "layer 1"
  142.                                 100
  143.                                 NORMAL
  144.                                )
  145.                            )
  146.                  )
  147.               ) ;end of our local variables
  148.  
  149.          <i class="replaceable"><tt>[Code here]</tt></i>
  150.        )
  151.       </pre>
  152.         <pre class="programlisting">
  153.  
  154.         (script-fu-register
  155.           "script-fu-text-box"                        ;func name
  156.           "<Toolbox>/Xtns/Script-Fu/Text/Text Box"    ;menu pos  
  157.           "Creates a simple text box, sized to fit\
  158.             around the user's choice of text,\
  159.             font, font size, and color."              ;description
  160.           "Michael Terry"                             ;author
  161.           "copyright 1997, Michael Terry"             ;copyright notice
  162.           "October 27, 1997"                          ;date created
  163.           ""                     ;image type that the script works on
  164.           SF-VALUE "Text:"         "\"Text Box\""     ;a text variable
  165.           SF-VALUE "Font:"         "\"Charter\""      ;a text variable
  166.           SF-VALUE "Font size:"       "45"               ;a text variable
  167.           SF-COLOR "Color:"        '(0 0 0)           ;color variable
  168.           SF-VALUE "Buffer amount" "35"               ; 0-100 % of text  
  169.         )
  170.  
  171.       </pre>
  172.       </div>
  173.       <div class="simplesect" lang="en" xml:lang="en">
  174.         <div class="titlepage">
  175.           <div>
  176.             <div>
  177.               <h4 class="title"><a id="id3431252"></a>Adding The New Code</h4>
  178.             </div>
  179.           </div>
  180.         </div>
  181.         <p>
  182.         We're going to add code in two places: right before we resize
  183.         the image, and at the end of the script (to return the new
  184.         image, the layer and the text). 
  185.       </p>
  186.         <p>
  187.         After we get the text's height and width, we need to resize
  188.         these values based on the buffer amount specified by the
  189.         user. We won't do any error checking to make sure it's in the
  190.         range of 0-100% because it's not life-threatening, and because
  191.         there's no reason why the user can't enter a value like "200"
  192.         as the percent of buffer to add. 
  193.       </p>
  194.         <pre class="programlisting">
  195.         (set! theBuffer (* theImageHeight (inBufferAmount 100) ) )
  196.  
  197.         (set! theImageHeight (+ theImageHeight theBuffer theBuffer) )
  198.         (set! theImageWidth  (+ theImageWidth  theBuffer theBuffer) )     
  199.       </pre>
  200.         <p>
  201.         All we're doing here is setting the buffer based on the height
  202.         of the text, and adding it twice to both the height and width
  203.         of our new image. (We add it twice to both dimensions because
  204.         the buffer needs to be added to both sides of the text.) 
  205.       </p>
  206.         <p>
  207.         Now that we have resized the image to allow for a buffer, we
  208.         need to center the text within the image. This is done by
  209.         moving it to the (x, y) coordinates of (<tt class="varname">theBuffer</tt>,
  210.         <tt class="varname">theBuffer</tt>). I added this line after
  211.         resizing the layer and the image: 
  212.       </p>
  213.         <pre class="programlisting">
  214.         (gimp-layer-set-offsets theText theBuffer theBuffer)
  215.       </pre>
  216.         <p>
  217.         Go ahead and save your script, and try it out after refreshing
  218.         the database.
  219.       </p>
  220.         <p>
  221.       All that is left to do is return our image, the layer, and the
  222.       text layer. After displaying the image, we add this line: 
  223.       </p>
  224.         <pre class="programlisting">
  225.         (list theImage theLayer theText)
  226.       </pre>
  227.         <p>
  228.         This is the last line of the function, making this list
  229.         available to other scripts that want to use it.
  230.       </p>
  231.         <p>
  232.         To use our new text box script in another script, we could
  233.         write something like the following: 
  234.       </p>
  235.         <pre class="programlisting">
  236.         (set! theResult (script-fu-text-box 
  237.                          "Some text" 
  238.                          "Charter" "30"
  239.                          '(0 0 0)
  240.                          "35"
  241.                         )
  242.         )
  243.         (gimp-image-flatten (car theResult))       
  244.       </pre>
  245.         <p>
  246.         Congratulations, you are on your way to your Black Belt of Script-Fu!
  247.       </p>
  248.       </div>
  249.     </div>
  250.     <div class="navfooter">
  251.       <hr />
  252.       <table width="100%" summary="Navigation footer">
  253.         <tr>
  254.           <td width="40%" align="left"><a accesskey="p" href="ch02s10s05.html">Prev</a>┬á</td>
  255.           <td width="20%" align="center">
  256.             <a accesskey="u" href="ch02s10.html">Up</a>
  257.           </td>
  258.           <td width="40%" align="right">┬á<a accesskey="n" href="ch02s11.html">Next</a></td>
  259.         </tr>
  260.         <tr>
  261.           <td width="40%" align="left" valign="top">10.5.┬áGiving Our Script Some Guts┬á</td>
  262.           <td width="20%" align="center">
  263.             <a accesskey="h" href="index.html">Home</a>
  264.           </td>
  265.           <td width="40%" align="right" valign="top">┬á11.┬áGetting Unstuck</td>
  266.         </tr>
  267.       </table>
  268.     </div>
  269.   </body>
  270. </html>
  271.